/**
 * OSAmI: Open Source Ambient Intelligence
 * Servlets running on the gateway, for displaying data from the sensor nodes.
 * Copyright (C) Jonathan Ferreira 2010 (internship in LCIS-INPG, for a demo in the FR part of the OSAMI project)
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
 */
package org.osami.gateway.display.internal;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.Validate;
import org.osami.gateway.identity.Identity;
import org.osami.gateway.identity.IdentityProvider;
import org.osami.gateway.sensor.data.SenseData;
import org.osami.gateway.storage.ISenseDataStorage;
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;

@Component
@Instantiate
public class NodesViewServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Requires
    private ISenseDataStorage storage;

    @Requires
    private HttpContext context;

    @Requires
    private HttpService httpService;

    @Requires(optional = true)
    private IdentityProvider identityProvider;

    @Validate
    public void start() throws ServletException, NamespaceException {
        this.httpService.registerServlet(ServletPaths.NODES, this, null, this.context);
    }

    @Invalidate
    public void stop() {
        this.httpService.unregister(ServletPaths.NODES);
    }

    @Override
    synchronized public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String html = "";

        List<String> nodes = new ArrayList<String>();
        synchronized (this.storage) {
            // Fetch the list of all unique nodes
            for (SenseData sd : this.storage) {
                String senseId = sd.getSenseId();
                String[] t = senseId.split("/");
                senseId = t[0];
                if (!nodes.contains(senseId)) {
                    nodes.add(senseId);
                }
            }
        }

        html += "<H1>List of nodes</H1><hr/>\n";

        html += "<ul>\n";
        for (String nodeId : nodes) {
            String identityName = "";
            Identity identity = this.identityProvider.getBoundIdentity(nodeId);
            if (identity != null) {
                final String identityName2 = identity.getName();
                if (identityName2 != null && !identityName2.equals("")) {
                    identityName = identityName2;
                }
            }

            if (!identityName.equals("")) {
                html += "\t<li><a href=\"" + ServletPaths.NODE + "/" + nodeId + "\">" + identityName + " (" + nodeId + ")</a></li>";
            }
            else {
                html += "\t<li><a href=\"" + ServletPaths.NODE + "/" + nodeId + "\">" + nodeId + "</a></li>";
            }
        }
        html += "</ul>\n";
        html += "<hr/>\n<a href=\"" + ServletPaths.INDEX + "\">Go back to main page</a>\n";


        PrintWriter writer = response.getWriter();

        writer.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");
        writer.println("<html lang=\"en\">");

        // HTML Head
        // ---------------------------------------------------
        writer.println("<head>");
        writer.println(" <title>List of nodes</title>");
        writer.println("<link href=\"/resource/css/designSensor.css\" rel=\"stylesheet\" type=\"text/css\" />");
        writer.println("<meta http-equiv=\"refresh\" content=\"5\">");
        writer.println("</head>");

        // HTML Body
        // ---------------------------------------------------
        writer.println("<body>");

        writer.println(html);

        writer.println("</body>");
        writer.println("</html>");
    }
}
